
window.onload = function(){

	var init = {
		num:0,
		is_over:0
	}
	//关卡
	var level = 0;

	//初始化
	var c 			  = document.getElementById("canvas");
	var window_width  = document.getElementsByTagName('body')[0].scrollWidth;
	var window_height = document.getElementsByTagName('body')[0].scrollHeight;
	c.style.width 	  = window_width  + "px";
	c.style.height 	  = window_height + "px";
	// console.log(window_width);

	var c 	= document.getElementById("canvas");
	var cxt = c.getContext("2d");
	var img = newImg("assets/bg.jpg");
	var fps = 0;
	img.onload = function(){
		cxt.drawImage(img,0,0,window_width,window_height);
	}

	//我
	var me = {
		x : window_width/2,
		y: window_height/2,
		width : 40,
		height : 80,
		integral : 0,
		miss:0,
		status:1
	}
	meImg_1 = newImg("assets/lead.png");
	meImg_2 = newImg("assets/lead_1.png");
	meImg_3 = newImg("assets/lead_2.png");
	meImg_4 = newImg("assets/lead_3.png");


	//积分榜
	// var heart = document.getElementById("heart");
	var miss  = document.getElementById("miss");
	var miss  = document.getElementById("miss_over");
	init.setIntegral = function(){
		// heart.innerHTML = me.integral;
		miss.innerHTML = me.miss;
	}

	//奖励
	awards = new Array();
	awardImg1 = newImg("assets/award.png");

	//画奖励
	init.setAward = function(){
		// clearTimeout(fps);

		if (parseInt((Math.random()*320)%59) == 0) {
			awards.push(getAward(1));
			// console.log(awards);
		}
		for(a in awards){

			if(awards[a].y<window_height) {
				awards[a].y+=5;
			}

			// if(awards[a].y<600 && awards[a].joke==0) {
			// 	awards[a].y+=5;
			// 	awards[a].joke = 0;
			// }else{
			// 	awards[a].joke = 1;
			// 	awards[a].y = awards[a].y - 3;
			// }

			cxt.drawImage(awards[a].eimg,awards[a].x,awards[a].y,awards[a].ewidth,awards[a].eheight);


			//碰撞
			if (awards[a].y + awards[a].eheight > me.y-(me.height/2) + 10 &&
				awards[a].x + awards[a].ewidth > me.x-(me.width/2) + 5 &&
				awards[a].x < me.x + me.width/2 - 5 &&
				awards[a].y < me.y + me.height/2
				){
				init.over();//游戏结束
				// me.integral += awards[a].integral;
				// awards.splice(a,1);
			}

			// if (awards[a].y > 600 || awards[a].y < 0) {
			if (awards[a].y > window_height) {
				// console.log(me.miss);
				me.miss ++;
				awards.splice(a,1);
			};
		}
	}
	//奖励的对象
	function awardObject(integral,ewidth,eheight,eimg){
		this.x = parseInt(Math.random()*320+1);
		this.y = 0
		this.integral = integral;
		this.ewidth = ewidth;
		this.eheight = eheight;
		this.eimg = eimg;
		this.joke = 0;
	}
	//获取奖励
	function getAward(type){
		switch(type){
			case 1:
			return new awardObject(1,40,40,awardImg1);
		}
	}

	//画我
	init.setMe = function(){
		cxt.drawImage(init.getMeStatus(me.status),me.x-(me.width/2),me.y-(me.height/2),me.width,me.height);
		if (init.num % 20 == 0) {
			// console.log(me.status);
			if (me.status >= 4) {me.status = 1}
			else{me.status ++;}
		}
	}
	init.getMeStatus = function(status){
		switch(status){
			case 1:
				return meImg_1
			;
			case 2:
				return meImg_2
			;
			case 3:
				return meImg_3
			;
			case 4:
				return meImg_4
			;
		}
	}

	//画背景
	init.setBg = function(){
		// this.num = this.num>800?0:this.num++;
		if(this.num>window_height){this.num = 0;}else{this.num++;}
		cxt.drawImage(img,0,this.num,window_width,window_height);
		cxt.drawImage(img,0,this.num-window_height,window_width,window_height);
	}

	//刷新帧
	init.update = function(){
		if (this.is_over != 1) {
			init.setBg();
			init.setMe();
			init.setAward();
			init.setIntegral(); //设置积分榜

		}
	}

	init.over = function(){
		this.is_over = 1;
		miss_over.innerHTML = me.miss;
		clearTimeout(fps);
		document.getElementById("gameover").style.display = "inline";
		document.title = "人活一世，我成功错过了"+me.miss+"次真爱，你呢？";
	}

	//时钟
	fps = setInterval(init.update,1/100);

	//跟踪鼠标
	c.addEventListener("mousemove",function(e){
		me.x = e.layerX;
		me.y = e.layerY;
		init.update();
	})

	//xy跟踪 临时
	// var coordinate = document.getElementById('coordinate');
	//跟踪触摸
	c.addEventListener("touchmove",function(e){

		// console.log(e.targetTouches[0]);

		me.x = e.targetTouches[0].pageX;
		me.y = e.targetTouches[0].pageY;
		init.update();

		// coordinate.innerHTML = "X："+me.x+"Y:"+me.y;
	})
}




document.body.addEventListener('touchmove', function(e) {
	e.stopPropagation();
	e.preventDefault();
});

function newImg(src){
	var obj = new Image();
	obj.src = src;
	return obj;
}